package logic

import (
	"context"
	"doctor-system/doctorrpc/doctorrpc"
	"doctor-system/doctorrpc/internal/svc"
	"doctor-system/models/mysql"
	"fmt"

	"github.com/zeromicro/go-zero/core/logx"
)

type AddDoctorLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAddDoctorLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddDoctorLogic {
	return &AddDoctorLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *AddDoctorLogic) AddDoctor(in *doctorrpc.AddDoctorRequest) (*doctorrpc.AddDoctorResponse, error) {
	// todo: add your logic here and delete this line
	var finDoctor mysql.Doctor
	mysql.DB.Where("name = ?", in.Name).Find(&finDoctor)
	if finDoctor.ID != 0 {
		return nil, fmt.Errorf("医师姓名唯一")
	}

	var doctor mysql.Doctor
	doctor = mysql.Doctor{
		Name:            in.Name,
		Image:           in.Image,
		Hospital:        in.Hospital,
		RegisterFee:     float64(in.RegisterFee),
		ConsultationFee: float64(in.ConsultationFee),
		Profile:         in.Profile,
	}
	mysql.DB.Debug().Create(&doctor)

	return &doctorrpc.AddDoctorResponse{Id: int64(doctor.ID)}, nil
}
